import os
import xml.etree.ElementTree as ET


def f(label, p):
    in_file = open(os.path.join(label, p))  # 需要转化的标签路径
    # print(in_file)
    tree = ET.parse(in_file)  # 解析xml文件
    root = tree.getroot()  # 获取xml文件的根节点

    for obj in root.iter('object'):
        # print(obj)
        obj.set("name", "safety_hat")

    tree.write(os.path.join('new_xml', p))


if __name__ == "__main__":
    label_ = 'box'
    all_xml = os.listdir(label_)
    for i in all_xml:
        if i.split(".")[-1] != "xml":
            continue
        f(label_, i)  # 转化标注文件格式
